<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android," />





  <link rel="alternate" href="/atom.xml" title="结网之路" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="本章主要介绍了如何使用内嵌在 fragment 中的对话框，以及 fragment 之间如何传递数据
GitHub 地址：完成第12章

1. 使用 DialogFragment1.1 AppCompat 兼容库
Google 推出 AppCompat 兼容库是为了让所有Android用户都能体验到新特性。AppCompat兼容库能通过支持库的方式将部分最新系统的特色功能移植到Android旧版">
<meta property="og:type" content="article">
<meta property="og:title" content="Android编程权威指南（第二版）学习笔记（十二）—— 第12章 对话框">
<meta property="og:url" content="https://kniost.github.io/2016-11-23-cj0ajvour000k1js6die3v2kv/index.html">
<meta property="og:site_name" content="结网之路">
<meta property="og:description" content="本章主要介绍了如何使用内嵌在 fragment 中的对话框，以及 fragment 之间如何传递数据
GitHub 地址：完成第12章

1. 使用 DialogFragment1.1 AppCompat 兼容库
Google 推出 AppCompat 兼容库是为了让所有Android用户都能体验到新特性。AppCompat兼容库能通过支持库的方式将部分最新系统的特色功能移植到Android旧版">
<meta property="og:image" content="http://img.blog.csdn.net/20161124133415509">
<meta property="og:updated_time" content="2017-03-14T06:38:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android编程权威指南（第二版）学习笔记（十二）—— 第12章 对话框">
<meta name="twitter:description" content="本章主要介绍了如何使用内嵌在 fragment 中的对话框，以及 fragment 之间如何传递数据
GitHub 地址：完成第12章

1. 使用 DialogFragment1.1 AppCompat 兼容库
Google 推出 AppCompat 兼容库是为了让所有Android用户都能体验到新特性。AppCompat兼容库能通过支持库的方式将部分最新系统的特色功能移植到Android旧版">
<meta name="twitter:image" content="http://img.blog.csdn.net/20161124133415509">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://kniost.github.io/2016-11-23-cj0ajvour000k1js6die3v2kv/"/>





  <title> Android编程权威指南（第二版）学习笔记（十二）—— 第12章 对话框 | 结网之路 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">结网之路</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">临渊羡鱼，不如退而结网</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://kniost.github.io/2016-11-23-cj0ajvour000k1js6die3v2kv/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kniost">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="结网之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Android编程权威指南（第二版）学习笔记（十二）—— 第12章 对话框
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-11-23T16:04:10+08:00">
                2016-11-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android编程权威指南学习笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Android编程权威指南学习笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016-11-23-cj0ajvour000k1js6die3v2kv/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2016-11-23-cj0ajvour000k1js6die3v2kv/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>本章主要介绍了如何使用内嵌在 fragment 中的对话框，以及 fragment 之间如何传递数据</p>
<p>GitHub 地址：<br><a href="https://github.com/kniost/Android-Programming-Big-Nerd-Ranch-Guide-2nd/commit/20c265c6dd9e122fc169eed8dc90da60f275f360" target="_blank" rel="external">完成第12章</a></p>
</blockquote>
<h1 id="1-使用-DialogFragment"><a href="#1-使用-DialogFragment" class="headerlink" title="1. 使用 DialogFragment"></a>1. 使用 DialogFragment</h1><h2 id="1-1-AppCompat-兼容库"><a href="#1-1-AppCompat-兼容库" class="headerlink" title="1.1 AppCompat 兼容库"></a>1.1 AppCompat 兼容库</h2><blockquote>
<p>Google 推出 AppCompat 兼容库是为了让所有Android用户都能体验到新特性。AppCompat兼容库能通过支持库的方式将部分最新系统的特色功能移植到Android旧版本系统中。</p>
</blockquote>
<p>应该在 Porject Structure 中添加 appcompat-v7 的依赖。</p>
<h2 id="1-2-创建-DialogFragment"><a href="#1-2-创建-DialogFragment" class="headerlink" title="1.2 创建 DialogFragment"></a>1.2 创建 DialogFragment</h2><blockquote>
<p>建议将 <code>AlertDialog</code> 封装在 <code>DialogFragment</code> (Fragment的子类)实例中使用。当然，不使用<code>DialogFragment</code> 也可显示 <code>AlertDialog</code> 视图,但不推荐这样做。使用 <code>FragmentManager</code> 管理对话框,可以更灵活地显示对话框。</p>
<p>如果旋转设备，单独使用的 <code>AlertDialog</code> 会消失，而封装在 fragment 中的 <code>AlertDialog</code> 则不会有此问题(旋转后,对话框会被重建恢复)。</p>
</blockquote>
<ul>
<li>首先需要一个针对 dialog 的布局文件，如 DatePicker 作为根元素的 Dialog 用于选择日期。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">DatePicker</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></div><div class="line">	<span class="attr">android:id</span>=<span class="string">"@+id/dialog_date_date_picker"</span></div><div class="line">	<span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></div><div class="line">	<span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></div><div class="line">	<span class="attr">android:calendarViewShown</span>=<span class="string">"false"</span>&gt;</div><div class="line">	<span class="comment">&lt;!--旧版系统会使用 calenderViewShown 属性--&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">DatePicker</span>&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li>然后新建一个父类是 <code>DialogFragment</code> 的类 <code>DatePickerFragment</code>，重写其中的 <code>onCreateDialog</code> 方法，返回一个<code>AlertDialog</code></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">public</span> Dialog <span class="title">onCreateDialog</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">	<span class="comment">// 使用 LayoutInflater 引用布局文件创建用于显示 Dialog 的 View</span></div><div class="line">	View v = LayoutInflater.from(getActivity())</div><div class="line">                .inflate(R.layout.dialog_date, <span class="keyword">null</span>);</div><div class="line">                </div><div class="line">	<span class="keyword">return</span> <span class="keyword">new</span> AlertDialog.Builder(getActivity())</div><div class="line">		.setView(v) <span class="comment">// 设置视图</span></div><div class="line">		.setTitle(R.string.date_picker_title) <span class="comment">//设置标题</span></div><div class="line">		<span class="comment">// 设置 OK 按钮，OnClickListener 暂时留空</span></div><div class="line">		.setPositiveButton(android.R.string.ok, <span class="keyword">null</span>)</div><div class="line">		<span class="comment">// 使用 Builder 的 create() 方法创建 Dialog 并返回</span></div><div class="line">		.create(); </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在使用 DialogFragment 时，使用成员方法 show 来显示 dialog：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 在 Fragment 中为 DatePickerFragment 添加一个 Tag</span></div><div class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String DIALOG_DATE = <span class="string">"DialogDate"</span>;</div><div class="line">……</div><div class="line"><span class="comment">// 在 Fragment 内部获取 FragmentManager</span></div><div class="line">FragmentManager manager = getFragmentManager(); </div><div class="line">DatePickerFragment dialog = <span class="keyword">new</span> DatePickerFragment();</div><div class="line"><span class="comment">// 显示对话框</span></div><div class="line">dialog.show(manager, DIALOG_DATE);</div></pre></td></tr></table></figure>
<h1 id="2-fragment-之间的数据传递"><a href="#2-fragment-之间的数据传递" class="headerlink" title="2. fragment 之间的数据传递"></a>2. fragment 之间的数据传递</h1><p>我们之前实现了 activity 之间以及基于 fragment 的 activity 之间的数据传递。现在需实现同一 activity 托管的两个 fragment 之间的数据传递。<br><img src="http://img.blog.csdn.net/20161124133415509" alt="一个 activity 托管的不同 fragment 之间数据的传递"></p>
<h2 id="2-1-将数据传递到对话框"><a href="#2-1-将数据传递到对话框" class="headerlink" title="2.1 将数据传递到对话框"></a>2.1 将数据传递到对话框</h2><p>显然，要达到目的，只需要在 DatePickerFragment 中建立获取实例的 newInstance 方法，其中需要的参数是传递的信息即可，示例如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// DatePickerFragment.java</span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> DatePickerFragment <span class="title">newInstance</span><span class="params">(Date date)</span> </span>&#123;</div><div class="line">	<span class="comment">// 新建一个 Bundle 对象用于存放数据</span></div><div class="line">	Bundle args = <span class="keyword">new</span> Bundle();</div><div class="line">	args.putSerializable(ARG_DATE, date);</div><div class="line">    DatePickerFragment fragment = <span class="keyword">new</span> DatePickerFragment();</div><div class="line">    <span class="comment">// 使用 fragment arguments 来传递参数</span></div><div class="line">    fragment.setArguments(args);</div><div class="line">    <span class="keyword">return</span> fragment;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>记得把使用 DatePickerFragment 的构造方法的地方改成 newInstance 方法获取实例。</p>
<p>在获得数据之后，要先将 DatePicker 初始化为原本的日期，首先用 Fragment 的 <code>getArguments().getSeriallizable(String key)</code> 方法获取数据，然后用 Calendar 对象取出 date 中的年月日，最后使用 DatePicker 类的 <code>init(int year, int month, int dayOfMonth, OnDateChangedListener listener)</code> 方法 初始化默认日期</p>
<h2 id="2-2-从对话框回传数据"><a href="#2-2-从对话框回传数据" class="headerlink" title="2.2 从对话框回传数据"></a>2.2 从对话框回传数据</h2><h3 id="2-2-1-设置目标-fragment"><a href="#2-2-1-设置目标-fragment" class="headerlink" title="2.2.1 设置目标 fragment"></a>2.2.1 设置目标 fragment</h3><p>类似于 activity 间的关联，可将 CrimeFragment 设置成 DatePickerFragment 的目标 fragment。 即使是在 CrimeFragment 和 DatePickerFragment 被销毁和重建后，操作系统也会重新关联它们。调用以下 Fragment 方法可建立这种关联:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTargetFragment</span><span class="params">(Fragment fragment, <span class="keyword">int</span> requestCode)</span></span></div></pre></td></tr></table></figure>
<p>该方法有两个参数：目标 fragment 以及请求代码。需要时，目标 fragment 使用请求代码确认是哪个 fragment 在回传数据。</p>
<p>目标 fragment 和请求代码由 FragmentManager 负责跟踪管理,我们可调用设置目标的 fragment 的 <code>getTargetFragment()</code> 和 <code>getTargetRequestCode()</code> 方法获取它们。</p>
<h3 id="2-2-2-传递数据给目标-Fragment"><a href="#2-2-2-传递数据给目标-Fragment" class="headerlink" title="2.2.2 传递数据给目标 Fragment"></a>2.2.2 传递数据给目标 Fragment</h3><p>处理由同一 activity 托管的两个 fragment 间的数据返回时，可借用<code>Fragment.onActivityResult(...)</code>方法。因此，直接调用目标 fragment 的<code>Fragment.onActivityResult(...)</code>方法,，就能实现数据的回传。该方法恰好有我们需要的如下信息：</p>
<ul>
<li>请求代码：与传入<code>setTargetFragment(...)</code>方法相匹配,告诉目标 fragment 返回结果来 自哪里。</li>
<li>结果代码：决定下一步该采取什么行动。 </li>
<li>Intent：包含 extra 数据。</li>
</ul>
<p>所以从 CrimeFragment 中显示 DatePickerFragment，用户选择日期以后，想要回传信息，可以写一个 sendResult 方法，该方法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// DatePickerFragment.java</span></div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String EXTRA_DATE =</div><div class="line">	<span class="string">"com.kniost.criminalintent.date"</span>;</div><div class="line"> ……                </div><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">sendResult</span><span class="params">(<span class="keyword">int</span> resultCode, Date date)</span> </span>&#123;</div><div class="line">	<span class="comment">// 防止出错</span></div><div class="line">	<span class="keyword">if</span> (getTargetFragment() == <span class="keyword">null</span>) &#123;</div><div class="line">		<span class="keyword">return</span>; </div><div class="line">	&#125;</div><div class="line">	Intent intent = <span class="keyword">new</span> Intent();</div><div class="line">	<span class="comment">// 放置数据到 Intent 中</span></div><div class="line">	intent.putExtra(EXTRA_DATE, date);</div><div class="line">	<span class="comment">// 获取目标 fragment，调用其 onActivityResult 方法，其中 RequestCode 是用 getTargetRequestCode 方法获取的，resultCode 是传入参数，intent 包含了数据</span></div><div class="line">	getTargetFragment()</div><div class="line">		.onActivityResult(getTargetRequestCode(), resultCode, intent);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在 CrimeFragment 中则应该重写 onActivityResult 方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// CrimeFragment.java</span></div><div class="line"></div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onActivityResult</span><span class="params">(<span class="keyword">int</span> requestCode, <span class="keyword">int</span> resultCode, Intent data)</span> </span>&#123;</div><div class="line">	<span class="comment">// 如果一切正常，调用 sendResult 的方法时传入的参数应该就是 Activity.RESULT_OK，所以不会直接 return</span></div><div class="line">    <span class="keyword">if</span> (resultCode != Activity.RESULT_OK) &#123;</div><div class="line">        <span class="keyword">return</span>;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	<span class="comment">// 如此判断方便有多个回传时使用</span></div><div class="line">	<span class="keyword">if</span> (requestCode == REQUEST_DATE) &#123;</div><div class="line">		Date date = (Date) data</div><div class="line">			.getSerializableExtra(DatePickerFragment.EXTRA_DATE);</div><div class="line">		mCrime.setDate(date); mDateButton.setText(mCrime.getDate().toString());</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="3-挑战练习"><a href="#3-挑战练习" class="headerlink" title="3. 挑战练习"></a>3. 挑战练习</h1><p><code>//待完成</code></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag"># Android</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016-11-17-cj0ajvoun000g1js6ral6bwd6/" rel="next" title="Android编程权威指南（第二版）学习笔记（十一）—— 第11章 使用 ViewPager">
                <i class="fa fa-chevron-left"></i> Android编程权威指南（第二版）学习笔记（十一）—— 第11章 使用 ViewPager
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016-11-24-cj0ajvout000n1js69l6ox3jb/" rel="prev" title="Android编程权威指南（第二版）学习笔记（十三）—— 第13章 工具栏（Toolbar）">
                Android编程权威指南（第二版）学习笔记（十三）—— 第13章 工具栏（Toolbar） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread"></div>
<script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kniost-comment-github.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
                                
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.jpg"
               alt="kniost" />
          <p class="site-author-name" itemprop="name">kniost</p>
           
              <p class="site-description motion-element" itemprop="description">kniost 的成长与疑惑</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">31</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-使用-DialogFragment"><span class="nav-text">1. 使用 DialogFragment</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-AppCompat-兼容库"><span class="nav-text">1.1 AppCompat 兼容库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-创建-DialogFragment"><span class="nav-text">1.2 创建 DialogFragment</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-fragment-之间的数据传递"><span class="nav-text">2. fragment 之间的数据传递</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-将数据传递到对话框"><span class="nav-text">2.1 将数据传递到对话框</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-从对话框回传数据"><span class="nav-text">2.2 从对话框回传数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-1-设置目标-fragment"><span class="nav-text">2.2.1 设置目标 fragment</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-2-传递数据给目标-Fragment"><span class="nav-text">2.2.2 传递数据给目标 Fragment</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-挑战练习"><span class="nav-text">3. 挑战练习</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kniost</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'kniost-comment-github';
      var disqus_identifier = '2016-11-23-cj0ajvour000k1js6die3v2kv/';

      var disqus_title = "Android编程权威指南（第二版）学习笔记（十二）—— 第12章 对话框";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      
        var disqus_config = function () {
            this.page.url = disqus_url;
            this.page.identifier = disqus_identifier;
            this.page.title = disqus_title;
        };
        run_disqus_script('embed.js');
      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
